package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.QaListBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.MainForum2Adapter;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/6/28.
 */
public class MainForum2Fragment extends Fragment {

    List<QaListBean.DataBean> mList;
    MainForum2Adapter mAdapter;
    UserPresenter mPresenter;

    @Bind(R.id.rv_main_forum2)
    ListView mRvMainForum2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_forum2, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        initRv();
        mPresenter = new UserPresenterImp();
    }

    @Override
    public void onStart() {
        super.onStart();


    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initRv() {
        mList = new ArrayList();
        mAdapter = new MainForum2Adapter(getActivity(), mList);

        mRvMainForum2.setAdapter(mAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void onEventMainThread(QaListBean bean) {
        if (bean.getReturn_code() == 0) {
            mList.clear();
            mList.addAll(bean.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            DeviceUtil.MyToast(getContext(), bean.getMsg());
        }
    }
}
